<?php
/**
 * Created by Zhy
 * User: Zhy
 * Date: 2020/2/13
 * Time: 14:39
 */

namespace app\admin\controller;


use think\Db;
use think\Request;

class Casescopy extends Base
{
    /**
     * 首页
     */
    public function index(){
        if(request()->isAjax()){
            extract(input());
            $where = [
                'case_verify' => ['<>',4]
            ];
            if(isset($username) && $username != ''){
                $where['user_username|user_phone'] = ['like', '%' . $username . '%'];
            }
            if(isset($user_name) && $user_name != ''){
                $where['case_name|case_address|case_info'] = ['like', '%' . $user_name . '%'];
            }
            if(isset($user_name2) && $user_name2 != ''){
                $where['case_username|case_username2|case_phone|case_phone2'] = ['like', '%' . $user_name2 . '%'];
            }
            if(isset($case_verify) && $case_verify != ''){
                $where['case_verify'] = $case_verify;
            }
            $NowPage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::table('up_case_copy c')
                ->join('up_u_user u', 'c.case_user_id = u.user_id', 'left')
                ->where($where)
                ->count();// 获取总条数
            $field = 'c.*,user_username,user_phone';
            $lists = Db::table('up_case_copy c')
                ->join('up_u_user u', 'c.case_user_id = u.user_id', 'left')
                ->field($field)
                ->where($where)
                ->page($NowPage,$limits)
                ->order('case_time desc')
                ->select();
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        return $this->fetch();
    }
    /**
     * 编辑
     * @return mixed
     */
    public function newedit(){
        if(Request::instance()->isPost()){
            extract(input());
//            if($case_uids) {
//                Db::table('up_u_user')->where('user_id','in',$case_uids)->update(['user_order_time' => date('Y-m-d H:i:s')]);
//                $case_uids = ','.$case_uids.',';
//            }
            $data = [
                'case_name' => $case_name,
                'case_qu' => $case_qu,
                'case_address' => $case_address,
//                'case_banner' => $case_banner,
//                'case_video' => $case_video,
                'case_two_pic' => $case_two_pic,
//                'case_imgs' => $case_imgs,
//                'case_photo' => $case_photo,
                'case_info' => $case_info,
                'case_jindu' => $case_jindu,
                'case_username' => $case_username,
                'case_username2' => $case_username2,
                'case_username3' => $case_username3,
                'case_username4' => $case_username4,
                'case_username5' => $case_username5,
                'case_phone' => $case_phone,
                'case_phone2' => $case_phone2,
                'case_phone3' => $case_phone3,
                'case_phone4' => $case_phone4,
                'case_phone5' => $case_phone3,
                'case_company' => $case_company,
                'case_shejishi' => $case_shejishi,
//                'case_uids' => $case_uids,
                'case_excel' => $case_excel,
                'case_excel_phone' => $case_excel_phone,
                'case_excel_uids' => ','.$case_excel_uids.',',
                'case_room' => $case_room,
                'case_style' => $case_style,
                'case_size' => $case_size,
                'case_sort' => $case_sort,
                'case_company_letter' => '',
                'case_shejishi_letter' => '',
                'case_shejishi_have' => 1,
//                'case_time' => date('Y-m-d H:i:s')
            ];
            $lonlat = explode(',',$lonlat);
            $data['case_lat'] = $lonlat[0];
            $data['case_lon'] = $lonlat[1];
            $str = trim($case_name,' ');
            $str= iconv("UTF-8","gb2312//IGNORE", $str);
            $tmp = bin2hex(substr($str,0,1));
            if($tmp>='B0'){ //汉字的开始
                $t=$this->getLetter(hexdec(bin2hex(substr($str,0,2))));
                $letter = chr($t);
            }else{
                $letter = strtoupper(substr($str,0,1));
            }
            $letter = $this->getLetter2($letter,mb_substr(trim($case_name,' '),0,1));
            $data['case_letter'] = $letter;
//            if($seeids == 1){
//                $data['case_see_uids'] = -1;
//            }else{
//                if(!isset($case_see_uids) || !$case_see_uids) return json(['code' => 100, 'msg' => '请选择商家']);
//                $data['case_see_uids'] = ',' . $case_see_uids . ',';
//            }
            //--------------------------字母------------------------------------------
            if($case_company){
                $letter = $this->userLetter(trim($case_company,' '));
                $data['case_company_letter'] = $letter;
            }
            if($case_shejishi){
                $letter = $this->userLetter(trim($case_shejishi,' '));
                $data['case_shejishi_letter'] = $letter;
                $data['case_shejishi_have'] = 2;
            }
            $res = Db::table('up_case_copy')->where('case_id',$case_id)->update($data);
            if($res){
                return json(['code' => 200, 'msg' => '操作成功']);
            }else{
                return json(['code' => 100, 'msg' => '操作失败']);
            }
        }
        $case_id = input('case_id');
        $info = Db::name('case_copy')
            ->where('case_id',$case_id)
            ->find();
        if($info['case_see_uids'] == -1) {
            $sel = 1;
        }else{
            $sel = 2;
        }
        $info['case_see_uids'] = explode(',',trim($info['case_see_uids'],','));
        $info['case_excel_uids'] = explode(',',trim($info['case_excel_uids'],','));
        $info['case_uids'] = explode(',',trim($info['case_uids'],','));
        $res = $this->getoptions();
        $this->assign('qus',$res[0]);
        $this->assign('room',$res[1]);
        $this->assign('style',$res[2]);
        $this->assign('size',$res[3]);
        $this->assign('users',$res[4]);
        $this->assign('jindu',$res[5]);
        $this->assign('info',$info);
        $this->assign('sel',$sel);
        $pps = Db::table('up_u_user')
            ->where('user_status',1)
            ->where('user_type','like','%2%')
            ->field('user_id,user_d_name,user_username,user_d_company,user_phone')
            ->order('user_letter asc')
            ->select();
        $this->assign('pps',$pps);
        return $this->fetch();
    }
    public function getoptions()
    {
        $qus = Db::table('up_area')
            ->where('pid',108)
            ->field('district_id,district')
            ->select();
        $room = Db::table('up_room')
            ->field('room_id,room_name')
            ->order('room_sort desc')
            ->select();
        $style = Db::table('up_style')
            ->field('style_id,style_name')
            ->order('style_sort desc')
            ->select();
        $size = Db::table('up_size')
            ->field('size_id,size_name')
            ->order('size_id asc')
            ->select();
        $users = Db::table('up_u_user')
            ->where('user_type','like','%3%')
            ->field('user_id,user_username,user_phone')
            ->select();
        $jindu = Db::table('up_jindu')
            ->where('jindu_status',1)
            ->field('jindu_id,jindu_name')
            ->order('jindu_sort desc')
            ->select();
        $res = [$qus,$room,$style,$size,$users,$jindu];
        return $res;
    }
    /**
     * 同意
     * @return \think\response\Json
     */
    public function agree(){
        $new_id = input('id');
        $data = Db::table('up_case_copy')->where('case_id',$new_id)->find();
        if($data['case_verify'] != 1) return json(['code' => 100, 'msg' => '只有待审核的才可以同意！']);
        Db::startTrans ();
        try{
            unset($data['case_id']);
            unset($data['case_user_id']);
            unset($data['case_verify']);
            unset($data['case_verify_time']);
            unset($data['case_verify_remark']);
            Db::table('up_case')->insert($data);
            Db::table('up_case_copy')->where('case_id',$new_id)->update([
                'case_verify' => 2,
                'case_verify_time' => date('Y-m-d H:i:s'),
                'case_verify_remark' => ''
            ]);
            Db::commit ();
            return json(['code' => 200, 'msg' => '操作成功']);
        }catch (\Exception $e){
            Db::rollback ();
            return json(['code' => 100, 'msg' => '操作失败']);
        }
    }
    /**
     * 拒绝
     * @return mixed
     */
    public function refuse(){
        if(Request::instance()->isPost()){
            extract(input());
            $case_verify = Db::table('up_case_copy')->where('case_id',$case_id)->value('case_verify');
            if($case_verify != 1) return json(['code' => 100, 'msg' => '只有待审核的才可以拒绝！']);
            $data = [
                'case_verify' => 3,
                'case_verify_remark' => $case_verify_remark,
                'case_verify_time' => date('Y-m-d H:i:s')
            ];
            $res = Db::table('up_case_copy')->where('case_id',$case_id)->update($data);
            if($res){
                return json(['code' => 200, 'msg' => '成功']);
            }else{
                return json(['code' => 100, 'msg' => '失败']);
            }
        }
        $case_id = input('case_id');
        $info = Db::name('case_copy')
            ->where('case_id',$case_id)
            ->find();
        $this->assign([
            'info' => $info,
        ]);
        return $this->fetch();
    }
}